package com.seaengine.proxy
{
	
	import com.seaengine.GameConfig;
	import com.seaengine.resourcemanager.ResSystem;
	import com.seaengine.staticdata.GYRefiningRule;
	import com.seaengine.valueobject.EquipCompositionVo;
	import com.seaengine.valueobject.EquipStrengthenRuleVo;
	import com.seaengine.valueobject.RefiningRuleVo;
	
	import flash.utils.Dictionary;
	

	public class EquipProxy
	{

		private var strengthenRules:Dictionary;
		private var compositionRules:Dictionary;
		private var refiningRules:Dictionary;

		public function init():void
		{
			ResSystem.getInstance().getRes(GameConfig.m_ResConfig + 'item/xml/enhancementRule.xml', onInit);
			ResSystem.getInstance().getRes(GameConfig.m_ResConfig + 'config/composition.xml', onInit);
			ResSystem.getInstance().getRes(GameConfig.m_ResConfig + 'item/xml/refiningRule.xml', onInit);
		}

		private function onInit(filename:String, data:Object):void
		{
			filename = filename.slice(filename.lastIndexOf('/') + 1);
			var xml:XML = data as XML;
			switch (filename)
			{
				case 'enhancementRule.xml':
					strengthenRules = new Dictionary;
					for each (var id:XML in xml.item.id)
					{
						strengthenRules[int(id)] = new EquipStrengthenRuleVo(id.@min, id.@max, id.@probability, id.@margin, id.@money, id.@ingot);
					}
					break;
				case 'composition.xml':
					compositionRules = new Dictionary;
					for each (var item:XML in xml.item)
					{
						var composition:EquipCompositionVo = new EquipCompositionVo;
						for each (var material:XML in item.materials)
						{
							composition.itemIDs.push(int(material.@item));
							composition.counts.push(int(material.@quantity));
						}
						composition.objectItem = item.id.@objectItem;
						composition.probability = item.id.@probability;
						compositionRules[int(item.id)] = composition;
					}
					break;
				case 'refiningRule.xml':
					refiningRules = new Dictionary;
//					for each (id in xml.item.id)
//					{
//						var value:RefiningRuleVo = new RefiningRuleVo(id.@money, id.@ingot);
//						refiningRules[int(id)] = value;
//					}
					for each(item in xml.item)
					{
						var rr:GYRefiningRule = new GYRefiningRule(int(item.id));
						rr.loadXML(xml);
						
						var value:RefiningRuleVo = new RefiningRuleVo(rr.money, rr.ingot);
						value.props = rr.props;
						refiningRules[int(item.id)] = value;
					}
					break;
			}
		}

		public function getStrengthenRuleByLevel(level:int):EquipStrengthenRuleVo
		{
			var nextL:int = level;
			for each (var rule:EquipStrengthenRuleVo in strengthenRules)
			{
				if (rule.isIn(nextL))
					return rule;
			}
			return null;
		}
		
		public function getStrengthenRuleById(id:int):EquipStrengthenRuleVo
		{
			return strengthenRules[id];
		}

		/**
		 * 获取累计加成
		 * @param level
		 * @return
		 *
		 */
		public function getAcuumulateStrengthenRate(level:int):int
		{
			var rate:int = 0;
			for each (var rule:EquipStrengthenRuleVo in strengthenRules)
			{
				if (rule.isIn(level))
				{
					rate += rule.margin * (level - rule.min + 1);
					break;
				}
				else
					rate += rule.margin * (rule.max - rule.min + 1);
			}
			return rate;
		}

		public function getCompositionRule(ruleID:uint):EquipCompositionVo
		{
			return compositionRules[ruleID];
		}

		public function getRefiningRule(ruleID:int):RefiningRuleVo
		{
			return refiningRules[ruleID];
		}

		/**------------------------------------------------------*/
		public function EquipProxy(single:Singleton)
		{
		}

		private static var instance:EquipProxy;

		public static function getInstance():EquipProxy
		{
			if (!instance)
				instance = new EquipProxy(new Singleton);
			return instance;
		}
	}
}

class Singleton
{
}
